/*1942 game clone
 * Authors:
 * Abdollah Tabareh
 * Bharat Bhushan Konka
 * Martin Svensson 
 * Richard Zianne
 * Tigran Harutyunyan
 * 
 * 2010
*/

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace Game1942.Handelers
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Wave3Handeler : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private Texture2D _texture;
        private Game _game;
        private int _counter = 0;
        private Vector2 _waveDirection;
        private int _speed = 5;
        private float _randomPosition = 0;
        private GameScene _gameScene;

        protected List<Enemy3> _planes;
        public Wave3Handeler(Game game, ref Texture2D texture) : base(game)
        {
            _texture = texture;
            _planes = new List<Enemy3>();
            _game = game;
            _waveDirection = Direction.Down;
        }

        internal void Initialize(GameScene gameScene)
        {
            _gameScene = gameScene;
            Initialize();
        }
        
        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            //_planes.Clear;
            for (int i = 0; i < 1; i++)
            {
                AddPlane(i);
                //_randomPosition = Util.Instance.RandomGenerator.Next(Util.ScreenWidth);                
            }

            base.Initialize();
        }

        private void AddPlane(int planeNumber)
        {
            Enemy3 newEnemy = new Enemy3(_game, ref _texture);
            newEnemy._index = planeNumber;
            newEnemy.putInPosition(calculatePosition(planeNumber, newEnemy));
                       
            newEnemy._direction = Direction.Down;
            newEnemy._planeSpeed = _speed;
            _planes.Add(newEnemy);
            


        }

        private Vector2 calculatePosition(int planeNumber, Enemy3 enemy)
        {
            planeNumber++;
            Vector2 value = new Vector2();
            value.X = _randomPosition;
            value.Y = 0;    
        
            return value;
        }

        



        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            for (int i = 0; i < _planes.Count; i++)
            {
                if (_planes[i]._isDead)
                {
                    _planes[i]._position.Y = 0;
                    _planes[i]._isDead = false;
                    _planes[i]._isDying = false;
                    
                }
            }
            loop();
            //loopLeft();

           
            
                  
                   
            for (int i = 0; i < _planes.Count; i++)
            {
                _planes[i]._index = i;
                if (_planes[i].getPosition().Y == Util.ScreenHeight)
                {
                    _planes[i].putInPosition(calculatePosition(_planes[i]._index,_planes[i]));
                }

                
                _planes[i]._direction = _waveDirection;
                _planes[i]._planeSpeed = _speed;
                _planes[i].Update(gameTime);
                
                
            }
            
            base.Update(gameTime);
        }



        private void loop()
        {
            if (_counter > Util.ScreenHeight)
            {
                _randomPosition = _randomPosition = _gameScene.getPlayerPositionX();
                _counter = 0;

            }
            else
            {
                _waveDirection = Direction.Down;
                _counter += _speed * 2;
            }
                
        }

        /// <summary>
        /// Allows the game component draw your content in game screen
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            
            for (int i = 0; i < _planes.Count; i++)
            {
                _planes[i].Draw(gameTime);
            }

            base.Draw(gameTime);
        }
        public void CheckCollision()
        {

        }

        public Enemy3[] getEnemys()
        {
            return _planes.ToArray();
        }
    }
}